/*
 * 描述： <描述>
 * 修改人： rain
 * 修改时间： 2016年8月9日
 * 项目： rainhy-servicelogger
 */
package com.rainhy.servicelogger.context;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;

/**
 * 业务日志会话
 * 
 * @author rain
 * @version [版本号, 2016年8月9日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ServiceLoggerSession {
    
    /** 线程变量:当前日志会话 */
    private static ThreadLocal<ServiceLoggerSession> session = new ThreadLocal<ServiceLoggerSession>() {
        @Override
        protected ServiceLoggerSession initialValue() {
            return new ServiceLoggerSession();
        }
    };
    
    /** 获取业务日志会话 */
    public static ServiceLoggerSession getSession() {
        return session.get();
    }
    
    /**
     * 初始化业务日志会话
     * 
     * @param map
     */
    public static void init(HttpServletRequest request, HttpServletResponse response, Map<String, Object> attributes) {
        session.remove();
        ServiceLoggerSession localSession = session.get();
        localSession.setRequest(request);
        localSession.setResponse(response);
        localSession.setAttributes(attributes);
        localSession.setInit(true);
    }
    
    /** 移除业务日志会话 */
    public static void remove() {
        session.get().clear();  // 移除会话中的值
        session.remove();   // 移除会话
    }
    
    /** request */
    private HttpServletRequest request;
    
    /** response */
    private HttpServletResponse response;
    
    /** 属性 */
    private Map<String, Object> attributes;
    
    /** 是否已经初始化 */
    private boolean isInit;
    
    private ServiceLoggerSession() {
        super();
        this.attributes = new HashMap<>();
    }
    
    /** 清空业务日志中对象 */
    public void clear() {
        if (this.attributes != null) {
            this.attributes.clear();
        }
        this.request = null;
        this.response = null;
        this.attributes = null;
        this.isInit = false;
    }
    
    /** 获取业务日志中的属性值 */
    public Object getAttribute(String key) {
        return this.attributes.get(key);
    }
    
    /** 返回 request */
    public HttpServletRequest getRequest() {
        return request;
    }
    
    /** 返回 response */
    public HttpServletResponse getResponse() {
        return response;
    }
    
    /** 是否已经初始化 */
    public boolean isInit() {
        return isInit;
    }
    
    /** 设置属性值到业务日志会话中 */
    public void setAttribute(String key, Object value) {
        this.attributes.put(key, value);
    }
    
    /** 设置属性 */
    public void setAttributes(Map<String, Object> attributes) {
        if (MapUtils.isNotEmpty(attributes)) {
            this.attributes.putAll(attributes);
        }
    }
    
    /** 是否已经初始化 */
    public void setInit(boolean isInit) {
        this.isInit = isInit;
    }
    
    /** 设置 request */
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
    
    /** 设置 response */
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }
}
